<?php
Class Campaign extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->data['controller'] = 'campaign';
        $this->load->model('Mcampaign');
        $this->_per = $this->checkPerMission($this->session->userdata('admin'), $this->session->userdata('SessionPermisson'), 18, $this->session->userdata('admin_id'));
    }
    
    public function userecoupon()
    {
        if($this->_per->read != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        $this->data['id_active'] = $this->getParamUri(4,3);
        $this->load->library('pagination');
        $config['per_page'] = 10;
        $this->data['page'] = $this->data['pageCurrent'] = $this->getParamUri(4,3);
        $offset = (($this->getParamUri(4,3) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(4,3) - 1) * $config['per_page']) : 0;
        $data = $this->Mcampaign->getUserEcouponDB($config['per_page'],$offset);
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/'.'/'.$this->uri->segment(3).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 4;
        $config['uri_segment_page'] = $this->getParamUri(4,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];
        $this->load->library('form_validation');
        $this->form_validation->set_rules('check-delete', 'Mục cần xóa', 'required');
        
        if($this->form_validation->run() === TRUE)
        {
            if($this->_per->del != 4)
            {
                $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'manage/ecoupon/index');
                exit();
            }
            $idArray = $this->getParamArray('checkboxs');
            $run = $this->Mecoupon->deleteEcouponDB($idArray);
            if(intval($run) == 200)
            {
                die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>/*alert("Xóa thành công");*/window.location="'.$this->data['baseurl'].'manage/ecoupon/index";</script>');
            }
        }
        
        $this->data['action'] = 'userecoupon';
        $this->load->view('template/layout', $this->data);
    }
    
    public function activeuserecoupon()
    {
        if($this->_per->read != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        $this->data['id_active'] = $this->getParamUri(4,3);
        $this->load->library('pagination');
        $config['per_page'] = 10;
        $this->data['page'] = $this->data['pageCurrent'] = $this->getParamUri(4,3);
        $offset = (($this->getParamUri(4,3) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(4,3) - 1) * $config['per_page']) : 0;
        $data = $this->Mcampaign->getUserActiveEcouponDB($config['per_page'],$offset);
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/'.'/'.$this->uri->segment(3).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 4;
        $config['uri_segment_page'] = $this->getParamUri(4,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];

        $this->load->library('form_validation');
        $this->form_validation->set_rules('check-delete', 'Mục cần xóa', 'required');
        
        if($this->form_validation->run() === TRUE)
        {
            if($this->_per->del != 4)
            {
                $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'manage/ecoupon/index');
                exit();
            }
            $idArray = $this->getParamArray('checkboxs');
            $run = $this->Mecoupon->deleteEcouponDB($idArray);
            if(intval($run) == 200)
            {
                die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>/*alert("Xóa thành công");*/window.location="'.$this->data['baseurl'].'manage/ecoupon/index";</script>');
            }
        }
        
        $this->data['action'] = 'activeuserecoupon';
        $this->load->view('template/layout', $this->data);
    }
    
    public function getecoupon()
    {
        if($this->_per->read != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'manage/ecoupon/index');
            exit();
        }
        $code_post = $this->getParamString('code');
        $this->data['render_coupon_qrcode'] = $this->Mecoupon->checkCode($code_post);
        $this->data['img_qrcode'] = $this->Mecoupon->renderQrCode($this->data['render_coupon_qrcode']->coupon_qr, 'qrcode/tmp/');
    }
    
    public function adduserecoupon()
    {
        if($this->_per->add != 2)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        $this->load->library('form_validation');
        $this->form_validation->set_rules('code_coupon', 'Mã voucher', 'required');
		$this->form_validation->set_rules('user_create', 'Thành viên', 'required');
		
		$this->form_validation->set_rules('restaurant_id', 'Nhà hàng', 'required');
		$this->form_validation->set_rules('log_work_car', 'Ca làm việc', 'required');
		$this->form_validation->set_rules('log_bills', 'Số bills', 'required');
		$this->form_validation->set_rules('log_customers', 'Số khách', 'required');
		$this->form_validation->set_rules('log_revenue', 'Doanh thu', 'required');
		$this->form_validation->set_rules('log_customer', 'Tên khách hàng', 'required');
		$this->form_validation->set_rules('log_mobile', 'Số điện thoại', 'required');
		$this->form_validation->set_rules('log_gift', 'Nội dung voucher', 'required');
		
        $this->data['success'] = '';
        if($this->form_validation->run() === TRUE)
        {
			$restaurant_id = $this->getParamInt('restaurant_id');
			$log_work_car = $this->getParamString('log_work_car');
			$log_bills = $this->getParamString('log_bills');
			$log_customers = $this->getParamString('log_customers');
			$log_revenue = $this->getParamString('log_revenue');
			$log_customer = $this->getParamString('log_customer');
			$log_mobile = $this->getParamString('log_mobile');
			$log_gift = $this->getParamString('log_gift');
			
			$code_coupon = $this->getParamString('code_coupon');
			$user_create = $this->getParamInt('user_create');
            $run = $this->Mcampaign->addUserEcouponDB($this->session->userdata('admin_id'),$code_coupon,$user_create,$restaurant_id,$log_work_car,$log_bills,$log_customers,$log_revenue,$log_customer,$log_mobile,$log_gift);
            if(intval($run) == 200)
            {
                $this->data['success'] = 200;
            }else if(intval($run) == 201)
            {
                $this->data['success'] = 200;
            }
        }
        $this->data['action'] = 'adduserecoupon';
        $this->load->view('template/layout', $this->data);
    }
    
    public function viewuserecoupon()
    {
        if($this->_per->read != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        $this->data['data'] = $this->Mcampaign->getDetailCoupon($this->getParamUri(4,1));
        //$this->data['user'] = $this->Mcampaign->checkuser($this->data['data']['user_id']);
        $this->data['action'] = 'viewuserecoupon';
        $this->load->view('template/layout', $this->data);
    }
    
    public function viewusercode()
    {
        if($this->_per->read != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        $this->data['data'] = $this->Mcampaign->getDetailCode($this->getParamUri(4,1)); 
        //$this->data['user'] = $this->Mcampaign->checkuser($this->data['data']['user_id']);
        
        $this->data['action'] = 'viewusercode';
        $this->load->view('template/layout', $this->data);
    }
    
    
    public function searchuserecoupon()
    {
        if($this->_per->read != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        $code_coupon = $this->getParamString('code');
        $dt = $this->Mcampaign->getForm($code_coupon); //echo '<pre>'; die(''.print_r($this->data['data']));
		$this->data['data'] = $dt['data'];
        $this->data['ctent'] = $dt['ctent'];
		$this->data['listRestaurant'] = $this->Mcampaign->listRestaurant('');
		//$this->data['select_user'] = $this->Mcampaign->selectMember('');
		$this->load->view('template/campaign/searchuserecoupon', $this->data);
    }
    
    /**
     * 
     * 
     * @void Code
     * 
     */
    
    
    public function usercode()
    {
        if($this->_per->read != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        $this->data['id_active'] = $this->getParamUri(4,3);
        $this->load->library('pagination');
        $config['per_page'] = 10;
        $this->data['page'] = $this->data['pageCurrent'] = $this->getParamUri(4,3);
        $offset = (($this->getParamUri(4,3) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(4,3) - 1) * $config['per_page']) : 0;
        $data = $this->Mcampaign->getUserCodeDB($config['per_page'],$offset);
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/'.'/'.$this->uri->segment(3).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 4;
        $config['uri_segment_page'] = $this->getParamUri(4,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
        $this->data['data'] = $data['data'];
        
        $this->data['action'] = 'usercode';
        $this->load->view('template/layout', $this->data);
    }
    
    public function activeusercode()
    {
        if($this->_per->read != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        $this->data['id_active'] = $this->getParamUri(4,3);
        $this->load->library('pagination');
        $config['per_page'] = 10;
        $this->data['page'] = $this->data['pageCurrent'] = $this->getParamUri(4,3);
        $offset = (($this->getParamUri(4,3) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(4,3) - 1) * $config['per_page']) : 0;
        $data = $this->Mcampaign->getUserActiveCodeDB($config['per_page'],$offset);
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/'.'/'.$this->uri->segment(3).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 4;
        $config['uri_segment_page'] = $this->getParamUri(4,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
        $this->data['data'] = $data['data'];
        
        $this->data['action'] = 'activeusercode';
        $this->load->view('template/layout', $this->data);
    }
    
    public function addusercode()
    {
        if($this->_per->add != 2)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        $this->load->library('form_validation');
        $this->form_validation->set_rules('user', 'Thành viên', 'required');
		
		$this->form_validation->set_rules('restaurant_id', 'Nhà hàng', 'required');
		$this->form_validation->set_rules('log_work_car', 'Ca làm việc', 'required');
		$this->form_validation->set_rules('log_bills', 'Số bills', 'required');
		$this->form_validation->set_rules('log_customers', 'Số khách', 'required');
		$this->form_validation->set_rules('log_revenue', 'Doanh thu', 'required');
		$this->form_validation->set_rules('log_customer', 'Tên khách hàng', 'required');
		$this->form_validation->set_rules('log_mobile', 'Số điện thoại', 'required');
		$this->form_validation->set_rules('log_gift', 'Quà tặng', 'required');
		
        $this->data['success'] = '';
        if($this->form_validation->run() === TRUE)
        {
			$restaurant_id = $this->getParamInt('restaurant_id');
			$log_work_car = $this->getParamString('log_work_car');
			$log_bills = $this->getParamString('log_bills');
			$log_customers = $this->getParamString('log_customers');
			$log_revenue = $this->getParamString('log_revenue');
			$log_customer = $this->getParamString('log_customer');
			$log_mobile = $this->getParamString('log_mobile');
			$log_gift = $this->getParamString('log_gift');
			
            $run = $this->Mcampaign->addUserCodeDB($this->session->userdata('admin_id'),$restaurant_id,$log_work_car,$log_bills,$log_customers,$log_revenue,$log_customer,$log_mobile,$log_gift);
            if(intval($run) == 200)
            {
                $this->data['success'] = 200;
            }else if(intval($run) == 201)
            {
                $this->data['success'] = 200;
            }
        }
        $this->data['action'] = 'addusercode';
        $this->load->view('template/layout', $this->data);
    }
    
    public function searchusercode()
    {
        if($this->_per->read != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        $code_coupon = $this->getParamString('code');
        $this->data['data'] = $this->Mcampaign->getFormCode($code_coupon);
        //$this->data['select_user'] = $this->Mcampaign->selectMember('');
		$this->data['listRestaurant'] = $this->Mcampaign->listRestaurant('');
        $this->load->view('template/campaign/searchusercode', $this->data);
    }
    
    function reportusercode()
    {
        if($this->_per->read != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }

        $this->data['id_active'] = $this->getParamUri(4,3);
        $this->load->library('pagination');
        $this->data['per_page'] = $config['per_page'] = 10;
        $this->data['page'] = $this->data['pageCurrent'] = $this->getParamUri(4,3);
        $offset = (($this->getParamUri(4,3) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(4,3) - 1) * $config['per_page']) : 0;
        $restaurant_id = $this->getParamInt('restaurant_id');
        if(!in_array($restaurant_id, array(null, '', 0))){
            $data = $this->Mcampaign->getReportUserCode($config['per_page'],$offset,$restaurant_id);
        }else{
            $data = $this->Mcampaign->getReportUserCode($config['per_page'],$offset);
        }
        
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/'.$this->uri->segment(3).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 4;
        $config['uri_segment_page'] = $this->getParamUri(4,3);
        $config['suffix'] = '.html';
        if(!in_array($restaurant_id, array(null, '', 0))){
            $config['paramhack'] = '?restaurant_id='.$this->getParamInt('restaurant_id');
        }
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];
        
        $this->data['arrRestaurant'] = $this->Mcampaign->dbgetrestaurant();
        $this->data['selected'] = $this->getParamInt('restaurant_id');
        
        $this->data['action'] = 'reportusercode';
        $this->load->view('template/layout', $this->data);
    }
    
    public function ajaxreporttable()
    {
        $this->data['data'] = $this->Mcampaign->getReportExport();
        $this->data['action'] = 'ajaxreporttable';
        $this->load->view('template/campaign/ajaxreporttable', $this->data);
    }
	public function ajaxreporttablegiftcode()
    {
        $this->data['data'] = $this->Mcampaign->getReportExportgiftcode();
        $this->data['action'] = 'ajaxreporttablegiftcode';
        $this->load->view('template/campaign/ajaxreporttablegiftcode', $this->data);
    }
	
	
	
	
	public function fixemail(){
		if($this->_per->read != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
		$this->load->library('form_validation');
        $this->form_validation->set_rules('header1', 'Nội dung voucher', 'required');
		$this->form_validation->set_rules('content1', 'Lưu ý voucher', 'required');
		$this->form_validation->set_rules('header2', 'Nội dung quà tặng', 'required');
		$this->form_validation->set_rules('content2', 'Lưu ý quà tặng', 'required');
		$this->data['emailvoucher'] = $this->Mcampaign->EmailVoucher();
		$this->data['emailgift'] = $this->Mcampaign->EmailGift();
		if($this->form_validation->run() === TRUE)
        {
			$header1 = $this->getParamString('header1');
			$content1 = $this->getParamString('content1');
			$header2 = $this->getParamString('header2');
			$content2 = $this->getParamString('content2');
			
            $run = $this->Mcampaign->editFixEmail($header1,$content1,$header2,$content2);
            if(intval($run) == 200)
            {
                die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>alert("Sửa thành công");window.location="'.$this->data['baseurl'].'manage/campaign/fix-email";</script>');
            }else{
				die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>alert("ko tra ve 200");window.location="'.$this->data['baseurl'].'manage/campaign/fix-email";</script>');
			}
        }
        $this->data['action'] = 'fixmail';
        $this->load->view('template/layout', $this->data);
	}
	
	function reportuservoucher()
    {
        if($this->_per->read != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }

        $this->data['id_active'] = $this->getParamUri(4,3);
        $this->load->library('pagination');
        $this->data['per_page'] = $config['per_page'] = 10;
        $this->data['page'] = $this->data['pageCurrent'] = $this->getParamUri(4,3);
        $offset = (($this->getParamUri(4,3) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(4,3) - 1) * $config['per_page']) : 0;
        $restaurant_id = $this->getParamInt('restaurant_id');
        if(!in_array($restaurant_id, array(null, '', 0))){
            $data = $this->Mcampaign->getReportUserVoucher($config['per_page'],$offset,$restaurant_id);
        }else{
            $data = $this->Mcampaign->getReportUserVoucher($config['per_page'],$offset);
        }
        
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/'.$this->uri->segment(3).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 4;
        $config['uri_segment_page'] = $this->getParamUri(4,3);
        $config['suffix'] = '.html';
        if(!in_array($restaurant_id, array(null, '', 0))){
            $config['paramhack'] = '?restaurant_id='.$this->getParamInt('restaurant_id');
        }
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];
        
        $this->data['arrRestaurant'] = $this->Mcampaign->dbgetrestaurant();
        $this->data['selected'] = $this->getParamInt('restaurant_id');
        
        $this->data['action'] = 'reportuservoucher';
        $this->load->view('template/layout', $this->data);
    }
	
}